package cn.jiakeyun.umeng_vertify

import android.app.Activity
import android.app.ProgressDialog
import android.content.Context
import android.util.Log
import androidx.annotation.NonNull
import com.umeng.umverify.listener.UMPreLoginResultListener
import com.umeng.umverify.listener.UMTokenResultListener

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

/** UmengVertifyPlugin */
class UmengVertifyPlugin : FlutterPlugin, MethodCallHandler ,ActivityAware{
    val channelName = "umeng_vertify"
    val eventChannelName = "umeng_vertify_event"

    private lateinit var channel: MethodChannel
    private lateinit var eventChannel: EventChannel
    private lateinit var context: Context
    private lateinit var activity:Activity
    var events: EventChannel.EventSink? = null
    private val uVerifyManager: UVerifyManager by lazy {
        val manager = UVerifyManager()
        manager.run {
            initUMPreLoginResultListener(object : UMPreLoginResultListener {
                override fun onTokenSuccess(p0: String?) {
                    events?.success(mapOf("errorCode" to 0,"errorMessage" to "","resultTokenData" to p0))
                }

                override fun onTokenFailed(p0: String?, p1: String?) {
                    events?.success(mapOf("errorCode" to -1,"errorMessage" to p0,"data" to p1))
                }
            })
            initUMTokenResultListener(object : UMTokenResultListener {
                override fun onTokenSuccess(p0: String?) {
                    events?.success(mapOf("errorCode" to 1,"errorMessage" to "","uMTokenData" to p0))
                }

                override fun onTokenFailed(p0: String?) {
                    events?.success(mapOf("errorCode" to -2,"errorMessage" to p0,"data" to p0))
                }
            })
            initDismissListener(object : UVerifyManager.DismissListener {
                override fun dismiss() {
                    channel.invokeMethod("verifyDismiss",null)
                }
            })
            UVerifyManagerConfig.initUMVerityManager(this)
        }
        manager
    }

    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        eventChannel = EventChannel(flutterPluginBinding.binaryMessenger, eventChannelName)
        eventChannel.setStreamHandler(object : EventChannel.StreamHandler {
            override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
                this@UmengVertifyPlugin.events = events
            }

            override fun onCancel(arguments: Any?) {
                this@UmengVertifyPlugin.events = null
            }
        })
        context = flutterPluginBinding.applicationContext
        channel = MethodChannel(flutterPluginBinding.binaryMessenger, channelName)
        channel.setMethodCallHandler(this)
        UVerifyManagerConfig.nativeViewClick2FlutterListener =
            object : UVerifyManagerConfig.NativeViewClick2FlutterListener {
                override fun click(methodName: String) {
                    channel.invokeMethod(methodName, null)
                }
            }
    }



    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
        var hasMethod: Boolean = false
        hasMethod = uVerifyManager.checkMethod(call, result, context)
        if (hasMethod) return
    }

    override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)

    }

    override fun onAttachedToActivity(binding: ActivityPluginBinding) {
        activity = binding.activity
    }

    override fun onDetachedFromActivityForConfigChanges() {
    }

    override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
    }

    override fun onDetachedFromActivity() {
    }
}
